package com.sinosoft.emplmis.jobless.web;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sinosoft.emplmis.jobless.entity.JoblessServerEntity;
import com.sinosoft.emplmis.jobless.service.JoblessServerService;
import com.sinosoft.emplmis.jobless.web.dto.JoblessServerData;
import com.sinosoft.framework.core.common.exception.BusinessException;
import com.sinosoft.framework.core.common.utils.ResultJson;

/**
 * 失业人员服务台账管理
 * @author
 *
 */
@Controller
@RequestMapping("jobless/server")
public class JoblessServerController {

	ObjectMapper mapper = new ObjectMapper();
	
	/**
	 * 失业人员服务台账页面跳转
	 */
	@RequestMapping(value="/ServiceMenu", method = RequestMethod.GET)
	public String  JoblessService(){
		return"jobless/server/joblessService";
	}
	

	/**
	 * 失业人员服务台账信息服务类
	 */
	@Autowired
	private JoblessServerService joblessServerService;
	
	/**
	 * 根据主键unemployedId查询相关失业人员的服务台账
	 * @return
	 */
	@RequestMapping(value="/getJoblessServiceList", method = RequestMethod.POST)
	@ResponseBody
	public ResultJson getJoblessServiceList(@RequestParam("unemployedId") String unemployedId){
		ResultJson result = new ResultJson();
		String success = "";
		String message = "";	
		List<JoblessServerData> resultList = new ArrayList<JoblessServerData>();
		List<JoblessServerEntity> joblessServerEntityList = new ArrayList<JoblessServerEntity>();
		try {
			joblessServerEntityList = joblessServerService.selectAll(unemployedId);
			for (JoblessServerEntity joblessServerEntity : joblessServerEntityList) {
				JoblessServerData joblessServerData = new JoblessServerData();
				//失业人员实体类转化为失业人员dto
				joblessServerData.toData(joblessServerData,joblessServerEntity);
				resultList.add(joblessServerData);
			}
			result.setTotal(joblessServerService.count(unemployedId));
			result.setRows(resultList);
			success= ResultJson.SUCCESS_YES;
			message= ResultJson.MESSAGE;
		} catch (Exception e) {
			success= ResultJson.SUCCESS_NO;
			message= "获取失败";
			e.printStackTrace();
		}finally {
			result.setSuccess(success);
			result.setMessage(message);
		}
		return result;
			
	}
	
	/**
	 * 添加失业人员服务信息
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value="/insertJoblessSer", method=RequestMethod.POST)
	@ResponseBody
	public String insertJoblessServiceInfo(JoblessServerData data){
		JoblessServerEntity Entity= new JoblessServerEntity();
	    ResultJson resultJson = new ResultJson();
	    String result = "";
	    try {
	    	data.setId(UUID.randomUUID().toString().trim().replaceAll("-", ""));
		    Entity.toEntityData(Entity, data, "");
		    joblessServerService.insert(Entity);
		    resultJson.setSuccess(ResultJson.SUCCESS_YES);
		    resultJson.setMessage(ResultJson.MESSAGE);
	    } catch (BusinessException e) {
	    	resultJson.setSuccess(ResultJson.SUCCESS_NO);
		    resultJson.setMessage(e.getMessage());
		    resultJson.setResults(null);
	    } catch (Exception e1){
	    	resultJson.setSuccess(ResultJson.SUCCESS_NO);
		    resultJson.setMessage("保存失败");
		    resultJson.setResults(null);
		    e1.printStackTrace();
	    } finally{
	    	try {
	    		result = mapper.writeValueAsString(resultJson);
		    }catch (IOException e) {
		    	e.printStackTrace();
		    }
		    return result;
	    }
	}
	
	/**
	 * 删除服务信息
	 */
	@RequestMapping(value="/deleteJoblessSer", method = RequestMethod.POST)
	@ResponseBody
	public String deleteJoblessServiceInfo(@RequestParam("id") String id){
		String result = "";
		ResultJson resultJson = new ResultJson(); 
		try {
			joblessServerService.deleteByPrimaryKey(id);
			resultJson.setSuccess(ResultJson.SUCCESS_YES);
			resultJson.setMessage(ResultJson.MESSAGE);
		} catch (Exception e) {
			resultJson.setSuccess(ResultJson.SUCCESS_NO);
			resultJson.setMessage("删除失败");
			e.printStackTrace();
		}finally{
			try {
				result = mapper.writeValueAsString(resultJson);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return result;
	}
	
	/**
	 * 回显失业人员指定服务台账记录
	 * @return
	 */
	@RequestMapping(value="/getJoblessSer", method = RequestMethod.POST)
	@ResponseBody
	public JoblessServerData getJoblessServiceInfo(@RequestParam("id") String Id){
		JoblessServerData joblessServerData = new JoblessServerData();
		JoblessServerEntity joblessServerEntity = joblessServerService.selectByPrimaryKey(Id);
		joblessServerData.toEntityData(joblessServerData, joblessServerEntity);
		return joblessServerData;
	}
	
	/**
	 * 修改保存失业人员服务台账
	 * @return
	 */
	@RequestMapping(value="/updateJoblessSer", method = RequestMethod.POST)
	@ResponseBody
	public String updateJoblessInfo(JoblessServerData record){
		String result = "";
		ResultJson resultJson = new ResultJson();
		JoblessServerEntity joblessServerEntity = new JoblessServerEntity();
		try {
			//将entity转化为实体类dto
			joblessServerEntity.toEntityData(joblessServerEntity, record, "");
			joblessServerService.updateByPrimaryKey(joblessServerEntity);
			resultJson.setSuccess(ResultJson.SUCCESS_YES);
			resultJson.setMessage(ResultJson.MESSAGE);
		} catch (Exception e) {
			resultJson.setSuccess(ResultJson.SUCCESS_NO);
			resultJson.setMessage("修改失败");
			e.printStackTrace();
		}finally{
			try {
				result = mapper.writeValueAsString(resultJson);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return result;
	}
	
}
